/*
 * @(#)TestsetBean.java
 *
 * ver 1.0 Oct 3, 2007 ggranum
 */
package org.testfire.agent.bean;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.testfire.agent.AgentProperties;
import org.testfire.agent.util.TestsetUtil;
import org.testfire.model.TestsetType;

public class TestsetBean
{
    private String userName;

    public TestsetBean(){}

    public TestsetBean( String userName )
    {

        this.userName = userName;
    }

     public List<TestsetType> getTestsets() throws IOException{
        List<TestsetType> testsets = new ArrayList<TestsetType>( );
        File userDir = AgentProperties.getInstance().getSaveDirectory();
        userDir = new File( userDir, userName );
        if ( !userDir.exists() )
        {
            return testsets;
        }
        List<File> filesList = getTestsetFiles( userDir );
        for ( File f : filesList )
        {
            testsets.add( TestsetUtil.getTestSet( f ));
        }
        return testsets;

     }

    public List<String> getTestsetPaths() throws IOException
    {
        List<String> testsets = new ArrayList<String>();
        File userDir = AgentProperties.getInstance().getSaveDirectory();
        userDir = new File( userDir, userName );
        if ( !userDir.exists() )
        {
            return testsets;
        }
        List<File> filesList = getTestsetFiles( userDir );
        for ( File f : filesList )
        {
            String s = f.getCanonicalPath();
            s = s.substring( s.indexOf( userName ) + userName.length() + 1 );
            s = s.replace( File.separatorChar, '.');
            testsets.add( s );
        }
        return testsets;
    }

    private List<File> getTestsetFiles( File dir )
    {
        File[] files = dir.listFiles();
        List<File> filesList = new ArrayList();
        for ( File f : files )
        {
            if ( f.isDirectory() )
            {
                filesList.addAll( getTestsetFiles( f ) );
            }
            else
            {
                filesList.add( f );
            }
        }
        return filesList;
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName( String userName )
    {
        this.userName = userName;
    }
}

// eof: TestsetBean.java